<?php
declare (strict_types = 1);

namespace app\admin\controller;

use think\facade\Request;
use app\common\Common;
use app\common\Error;
use app\admin\service\IndexService;

class Index extends Base
{
    private $service;

    public function __construct(IndexService $service)
    {
        parent::__construct();
        $this->service = $service;
    }

    // 
    public function index()
    {
        return view();
    }

    // 渲染后台首页
    public function welcome()
    {
        return view();
    }

    // 获取首页数据
    public function data()
    {
        $params = Request::param();

        $data = $this->service->getAdminHomePageData();

        return Common::success($data);
    }

    // 首页曲线图
    public function curve()
    {
        $params = Request::param();

        if (!empty($params['end_date']) && empty($params['start_date'])) {
            return Common::fail(Error::NO_START_DATE_SELECTED);
        }

        if (empty($params['end_date']) && !empty($params['start_date'])) {
            return Common::fail(Error::NO_END_DATE_SELECTED);
        }

        if (!empty($params['end_date']) && !empty($params['start_date'])) {
            $timeDiff = strtotime($params['end_date'] . ' +1 day') - strtotime($params['start_date']);
            // dump($timeDiff / (24 * 60 *60));die;
            if ($timeDiff / (24 * 60 *60) > 31) {
                return Common::fail(Error::TIME_INTERVAL_IS_TOO_LARGE);
            }
        }

        $data = $this->service->getAdminHomePageCurveData($params);

        return Common::success($data);
    }
}
